﻿using System;
using System.Windows.Forms;

namespace OpenNETCF.Windows.Forms
{
	public partial class InputBoxEx : Form
	{
		#region Delegates

		public delegate bool InputValidator(string input);

		#endregion

		public string OnValidationErrorMessage = "Invalid Input!";
		public string OnValidationErrorMessageTitle = "Error";

		public InputValidator Validator;

		public InputBoxEx()
		{
			InitializeComponent();
		}

		private void mnuDone_Click(object sender, EventArgs e)
		{
			if (Validator != null)
			{
				if (!Validator(InputField.Text))
				{
					MessageBox.Show(OnValidationErrorMessage, OnValidationErrorMessageTitle, MessageBoxButtons.OK,
					                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
					return;
				}
			}

			DialogResult = DialogResult.OK;
		}

		private void mnuCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

		public static string ShowInputBox(string title,string heading, string inputBoxText, string noteText)
		{
			return ShowInputBox(title, heading, inputBoxText, noteText, null, string.Empty, string.Empty);
		}

		public static string ShowInputBox(string title, string heading, string inputBoxText, string noteText, InputValidator validator, string errorMessageText, string errorMessageTitle)
		{
			InputBoxEx dlg = new InputBoxEx();
			dlg.Text = title;
			dlg.Heading.Text = heading;
			dlg.InputField.Text = inputBoxText;
			dlg.TextNote.Text = noteText;
			dlg.Validator = validator;
			dlg.OnValidationErrorMessage = errorMessageText;
			dlg.OnValidationErrorMessageTitle = errorMessageTitle;
			if (dlg.ShowDialog() == DialogResult.OK)
				return dlg.InputField.Text;
			else
				return null;
		}
	}
}